<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/8/30
 * Time: 9:41
 */
namespace app\apiteacher\controller;
use think\Db;
class Seek extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $user_id2 = input('user_id');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($user_id2,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    //求助列表
    public function index(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['course_user_id'] = $user_id;
        $res = Db::table('up_course')->alias('a')
            ->where($where)
            ->limit($num*$limit,$num)
            ->select();
        foreach ($res as $key=>&$vo){
            if($vo['course_type']==1){
                $user_username = Db::table('up_u_user')->where('user_id',$vo['course_teacher_id'])->value('user_username');
                $vo['user_username'] = $user_username;
            }
        }
        return  json(array('status'=>200,'msg'=>$res));
    }

    //求组详情
    public function detail(){
        extract(input());
        $id = input('param.id');
        $where['course_id'] = $id;
        $res = Db::table('up_course')->alias('a')
            ->join('up_subject us','us.subject_id = a.course_subject_id','left')
            ->field('us.subject_name,a.*')
            ->where($where)
            ->find();
        if($res['course_type']==1){
            $user_username = Db::table('up_u_user')->where('user_id',$res['course_teacher_id'])->value('user_username');
            $res['user_username'] = $user_username;
        }
        if($res['course_status']==2){
            $data = Db::table('up_course_reply')->where('reply_course_id',$res['course_id'])->find();
            $res['data'] = $data;
        }
        $map['evaluate_course_id'] = $id;
        $map['evaluate_user_id'] = $user_id;
        $result = Db::table('up_evaluate')->field('evaluate_star,evaluate_content')->where($map)->find();
        $res['result'] = $result;
        return  json(array('status'=>200,'msg'=>$res));
    }

    //评价
    public function evaluate(){
        extract(input());
        $id = input('param.reply_id');
        $where['reply_id'] = $id;
        $res = Db::table('up_course_reply')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.reply_user_id','left')
            ->field('uuu.user_username,uuu.user_gender,a.*')
            ->where($where)
            ->find();
        return  json(array('status'=>200,'msg'=>$res));
    }
    //评价插入
    public function evaluateAnswer(){
        extract(input());
        $star = input('param.star');
        $content = input('param.content');
        $reply_id = input('param.reply_id');
        $info = Db::table('up_course_reply')->where('reply_id',$reply_id)->find();
        $data['evaluate_user_id'] = $user_id;
        $data['evaluate_teacher_id'] = $info['reply_user_id'];
        $data['evaluate_star'] = $star;
        $data['evaluate_content'] = $content;
        $data['evaluate_time'] = date('Y-m-d H:i:s',time());
        $data['evaluate_reply_id'] = $reply_id;
        $data['evaluate_course_id'] = $info['reply_course_id'];
        $res = Db::table('up_evaluate')->insertGetId($data);
        Db::table('up_u_user')->where('user_id',$info['reply_user_id'])->setInc('user_sort',$star*0.7);
        if($res){
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"失败"));
        }
    }
}